/******************************************************************************\
*  Animeshell 1.0 Multimedia manager                                           *
*  Copyright (C) 2006-2007 Rémy Guillemette <shaikadzari@gmail.com>            *
*                                                                              *
*  This program is free software; you can redistribute it and/or modify        *
*  it under the terms of the GNU General Public License version 2 only as      *
*  published by the Free Software Foundation.                                  *
*                                                                              *
*  This program is distributed in the hope that it will be useful,             *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
*  GNU General Public License for more details.                                *
*                                                                              *
*  You should have received a copy of the GNU General Public License           *
*  along with this program; if not, write to the                               *
*  Free Software Foundation, Inc.,                                             *
*  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                   *
\******************************************************************************/

#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>

#include "sqlclassbase.h"

/*!
  \def HOST Host on which the database is running.
*/
#define HOST "localhost"
//

sqlclassbase::sqlclassbase(QString pDriver, QString pDatabase, QString pUser, QString pPasswd)
{
	m_Driver = pDriver; 
	m_Database = pDatabase; 
	m_User = pUser;
	m_Passwd = pPasswd;
}


bool sqlclassbase::OpenDatabase()
{
	db = QSqlDatabase::addDatabase( m_Driver );
	db.setDatabaseName( m_Database );
	db.setUserName( m_User );
	db.setPassword( m_Passwd );
	db.setHostName( HOST );
	
	if( !db.open() )
	{
		//db.lastError().showMessage( "An error occured. Please read the README file in the sqltable"
		//			     "dir for more information.\n\n" );
		return false;
	}
	
	return true;
}


bool sqlclassbase::CloseDatabase()
{
	db.commit();
	db.close();
	db.~QSqlDatabase();
	return true;
}

int sqlclassbase::LookForTable( QString pTableName )
{	
	QSqlQuery q( QString("SELECT COUNT(*) FROM %1").arg(pTableName), db );
	
	// qDebug() << pTableName;
	// qDebug() << q.lastError();
	
	if ( q.next() )
	{
		return (q.value(0).toInt());
	}
	
	return -1;
}

//
